package com.tydic.common.core.utils.file;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
public class ImageUtils {

    /**
     * 图片拼接
     *
     * @param imageFiles 图片列表
     * @param type       1 横向拼接， 2 纵向拼接
     *                   （注意：必须两张图片长宽一致）
     */
    public static void mergeImage(List<File> imageFiles, int type, File targetFile) throws IOException {
        BufferedImage[] images = new BufferedImage[imageFiles.size()];
        for (int i = 0; i < imageFiles.size(); i++) {
            File imageFile = imageFiles.get(i);
            images[i] = ImageIO.read(imageFile);
        }
        mergeImage(images, type, targetFile);
    }

    /**
     * 图片拼接
     *
     * @param images 图片数组
     * @param type   1 横向拼接， 2 纵向拼接
     *               （注意：必须两张图片长宽一致）
     */
    public static void mergeImage(BufferedImage[] images, int type, File targetFile) throws IOException {
        int len = images.length;
        int[][] imageArrays = new int[len][];
        for (int i = 0; i < len; i++) {
            int width = images[i].getWidth();
            int height = images[i].getHeight();
            imageArrays[i] = new int[width * height];
            imageArrays[i] = images[i].getRGB(0, 0, width, height, imageArrays[i], 0, width);
        }
        int newHeight = 0;
        int newWidth = 0;
        for (int i = 0; i < images.length; i++) {
            // 横向
            if (type == 1) {
                newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
                newWidth += images[i].getWidth();
            } else if (type == 2) {// 纵向
                newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
                newHeight += images[i].getHeight();
            }
        }
        if (type == 1 && newWidth < 1) {
            return;
        }
        if (type == 2 && newHeight < 1) {
            return;
        }
        BufferedImage imageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        int height_i = 0;
        int width_i = 0;
        for (int i = 0; i < images.length; i++) {
            if (type == 1) {
                imageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, imageArrays[i], 0, images[i].getWidth());
                width_i += images[i].getWidth();
            } else if (type == 2) {
                imageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), imageArrays[i], 0, newWidth);
                height_i += images[i].getHeight();
            }
        }
        //输出想要的图片
        ImageIO.write(imageNew, "png", targetFile);
    }

    /**
     * 图片拼接
     *
     * @param images 图片数组
     * @param type   1 横向拼接， 2 纵向拼接
     *               （注意：必须两张图片长宽一致）
     */
    public static byte[] mergeImage(BufferedImage[] images, int type) throws IOException {
        int len = images.length;
        int[][] imageArrays = new int[len][];
        for (int i = 0; i < len; i++) {
            int width = images[i].getWidth();
            int height = images[i].getHeight();
            imageArrays[i] = new int[width * height];
            imageArrays[i] = images[i].getRGB(0, 0, width, height, imageArrays[i], 0, width);
        }
        int newHeight = 0;
        int newWidth = 0;
        for (int i = 0; i < images.length; i++) {
            // 横向
            if (type == 1) {
                newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
                newWidth += images[i].getWidth();
            } else if (type == 2) {// 纵向
                newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
                newHeight += images[i].getHeight();
            }
        }
        if (type == 1 && newWidth < 1) {
            return new byte[0];
        }
        if (type == 2 && newHeight < 1) {
            return new byte[0];
        }
        BufferedImage imageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        int height_i = 0;
        int width_i = 0;
        for (int i = 0; i < images.length; i++) {
            if (type == 1) {
                imageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, imageArrays[i], 0, images[i].getWidth());
                width_i += images[i].getWidth();
            } else if (type == 2) {
                imageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), imageArrays[i], 0, newWidth);
                height_i += images[i].getHeight();
            }
        }
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            //输出想要的图片
            ImageIO.write(imageNew, "png", byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            log.error("转换图片异常", e);
        }
        return new byte[0];
    }
}
